package com.xy.annotaion.handler.aop.processor;

import com.xy.annotaion.handler.aop.advice.adapter.TestAdvisorAdapter;
import org.springframework.aop.framework.adapter.AdvisorAdapter;
import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * not OK for registering AdvisorAdapter,
 * because it's too late for handling advisor
 *
 * @author Dell
 */
//@Component
@Deprecated
public class TestAdvisorAdapterBeanPostProcessor implements BeanPostProcessor {


    private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();

    /**
     * Specify the AdvisorAdapterRegistry to register AdvisorAdapter beans with.
     * Default is the global AdvisorAdapterRegistry.
     *
     * @see GlobalAdvisorAdapterRegistry
     */
    public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) {
        this.advisorAdapterRegistry = advisorAdapterRegistry;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof TestAdvisorAdapter) {
            advisorAdapterRegistry.registerAdvisorAdapter((AdvisorAdapter) bean);
        }

        return bean;
    }
}
